﻿using System;
using System.IO;
using System.IO.Compression;

namespace Fuse8.ContentManagementFramework.Web.Caching
{
    internal class CmsResponseFilter : Stream
    {
        #region Fields

        private string _captureFilename;
        private Stream _captureFileStream;
        private Stream _chainedStream;  

        #endregion       

        #region Properties

        public string CaptureFilename
        {
            get
            {
                return _captureFilename;
            }
        } 

        #endregion

        #region Constructor

        internal CmsResponseFilter(Stream filterChain, string captureFilename, bool useGZip)
        {
            _chainedStream = filterChain;
            _captureFilename = captureFilename;

            _captureFileStream = new FileStream(captureFilename, FileMode.CreateNew, FileAccess.Write);
            
            if (useGZip)
            {
                _captureFileStream = new GZipStream(_captureFileStream, CompressionMode.Compress);
            }        
        } 

        #endregion

        #region Public methods

        public void StopFiltering(bool deleteDataFile)
        {
            if (_captureFileStream != null)
            {
                _captureFileStream.Close();
                _captureFileStream = null;
            }

            if (deleteDataFile)
            {
                CmsOutputCacheManager.ScheduleFileDeletion(_captureFilename);
            }
        } 

        #endregion

        #region Stream implementation

        public override bool CanRead
        {
            get 
            {
                return false;
            }
        }

        public override bool CanSeek
        {
            get 
            {
                return false;
            }
        }

        public override bool CanWrite
        {
            get 
            {
                return true;
            }
        }

        public override void Flush()
        {
            _chainedStream.Flush();

            if (_captureFileStream != null)
            {
                _captureFileStream.Flush();
            }
        }

        public override long Length
        {
            get { throw new NotImplementedException(); }
        }

        public override long Position
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            throw new NotImplementedException();
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotImplementedException();
        }

        public override void SetLength(long value)
        {
            throw new NotImplementedException();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            _chainedStream.Write(buffer, offset, count);

            if (_captureFileStream != null)
            {
                _captureFileStream.Write(buffer, offset, count);
            }
        }

        #endregion
    }
}
